﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class CorporateActionMap : EntityTypeConfiguration<CorporateAction> {
        public CorporateActionMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.eod_corporate_actions");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(a => a.CorporateActionId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_COA
            Property(a => a.SymbolId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COA", 1) { IsUnique = true }));
            Property(a => a.ExDate)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COA", 2) { IsUnique = true }));
            Property(a => a.CorporateActionTypeId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COA", 3) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // 1..n Symbol
            HasRequired<Symbol>(a => a.Symbol)
                .WithMany()
                .HasForeignKey(a => a.SymbolId);

            // 1..n Corporate Action Type
            HasRequired<CorporateActionType>(a => a.CorporateActionType)
                .WithMany()
                .HasForeignKey(a => a.CorporateActionTypeId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(a => a.CorporateActionId)
                .IsRequired()
                .HasColumnName("coa_id");

            Property(a => a.SymbolId)
                .IsRequired()
                .HasColumnName("coa_sym_id");
            
            Property(a => a.CorporateActionTypeId)
                .IsRequired()
                .HasColumnName("coa_cat_id");

            Property(a => a.ExDate)
                .IsRequired()
                .HasColumnName("coa_action_dt");

            Property(a => a.Amount)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("coa_amount");
        }
    }
}
